<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
 

class b2c_api_goods
{
    /**
     * 公开构造方法
     * @params app object
     * @return null
     */
    public function __construct($app)
    {        
        $this->app = $app;
    }
    
    /**
     * 取到所有货品的bn和store
     * @param null
     * @return string json
     */
    public function getAllProductsStore()
    {
        $obj_products = $this->app->model('products');
        $arr_products = $obj_products->getList('bn,store');
        
        return $arr_products;
    }
	
	/**
	 * 添加一个商品
	 * @param mixed sdf结构
	 * @param object handle object
	 * @return mixed 返回增加的结果
	 */
	public function add(&$sdf, &$thisObj)
	{
		if (!$sdf['name'] || $sdf['cat_id'])
		{
			$thisObj->send_user_error(app::get('b2c')->_('需要添加的商品信息不存在！'), array('iid'=>''));
		}
		
		$db = kernel::database();
		$transaction_status = $db->beginTransaction();
		if (!$goods_id = $this->goods_item_update($sdf,$msg))
		{
			$db->rollback();
			$thisObj->send_user_error($msg, array('iid'=>''));
		}
		
		$db->commit($transaction_status);
		
		/** 得到所有的sku_id **/
		$obj_product = $this->app->model('products');
		$tmp_products = $obj_product->getList('product_id,bn',array('goods_id'=>$goods_id));
		$str_sku_bns = "";
		$str_sku_ids = "";
		foreach ((array)$tmp_products as $arr_product)
		{
			$str_sku_ids .= $arr_produc['product_id'] . ",";
			$str_bns .= $arr_produc['bn'] . ",";
		}
		if ($str_sku_ids)
			$str_sku_ids = substr($str_sku_ids, 0, strlen($str_sku_ids)-1);
		if ($str_bns)
			$str_bns = substr($str_bns, 0, strlen($str_bns)-1);
		
		/** 获取商品修改时间 **/
		$obj_goods = $this->app->model('goods');
		$tmp_goods = $obj_goods->getList('last_modify',array('goods_id'=>$goods_id));
		
		return array('iid'=>$goods_id, 'sku_id'=>$str_sku_ids, 'sku_bn'=>$str_bns, 'created'=>$tmp_goods[0]['last_modify']);
	}
	
	/**
	 * 编辑一个商品
	 * @param mixed sdf结构
	 * @param object handle object
	 * @return mixed 返回增加的结果
	 */
	public function update(&$sdf, &$thisObj)
	{
		if (!$sdf['goods_id'])
		{
			$thisObj->send_user_error(app::get('b2c')->_('需要修改的商品信息不存在！'), array('iid'=>''));
		}
		
		$db = kernel::database();
		$transaction_status = $db->beginTransaction();
		if (!$goods_id = $this->goods_item_update($sdf, $msg))
		{
			$db->rollback();
			$thisObj->send_user_error($msg, array('iid'=>$sdf['goods_id']));
		}
		
		$db->commit($transaction_status);
		
		/** 得到所有的sku_id **/
		$obj_product = $this->app->model('products');
		$tmp_products = $obj_product->getList('product_id,bn',array('goods_id'=>$goods_id));
		$str_sku_bns = "";
		$str_sku_ids = "";
		foreach ((array)$tmp_products as $arr_product)
		{
			$str_sku_ids .= $arr_produc['product_id'] . ",";
			$str_bns .= $arr_produc['bn'] . ",";
		}
		if ($str_sku_ids)
			$str_sku_ids = substr($str_sku_ids, 0, strlen($str_sku_ids)-1);
		if ($str_bns)
			$str_bns = substr($str_bns, 0, strlen($str_bns)-1);
		
		/** 获取商品修改时间 **/
		$obj_goods = $this->app->model('goods');
		$tmp_goods = $obj_goods->getList('last_modify',array('goods_id'=>$goods_id));
		
		return array('iid'=>$sdf['order_bn'], 'sku_id'=>$str_sku_ids, 'sku_bn'=>$str_bns, 'created'=>$tmp_goods[0]['last_modify']);
	}
	
	/**
	 * 编辑商品
	 * @param array sdf数据
	 * @param string message
	 * @return boolean 成功还是失败
	 */
	private function goods_item_update(&$sdf, &$msg='')
	{
		$arr_sku_store = json_decode($sdf['sku_store'],1);		
		$goods_store = 0;
		$obj_math = kernel::single('ectools_math');
		foreach ((array)$arr_sku_store as $sku_store)
		{
			$goods_store = $obj_math->number_plus(array($goods_store,$sku_store));
		}
		
		$time = time();	
		
		$sdf_goods = array(
			'bn'=>$sdf['bn'] ? $sdf['bn'] : '',
			'name'=>$sdf['name'],
			'type'=>array(
				'type_id'=>$sdf['type_id'] ? $sdf['type_id'] : '',
			),
			'category'=>array(
				'cat_id'=>$sdf['cat_id'] ? $sdf['cat_id'] : '',
			),
			'brand'=>array(
				'brand_id'=>$sdf['brand_id'] ? $sdf['brand_id'] : '',
			),
			'uptime'=>$time,
			'last_modify'=>$time,
			'gain_score'=>$sdf['score'] ? $sdf['score'] : '',
			'unit'=>$sdf['unit'] ? $sdf['unit'] : '',
			'brief'=>$sdf['brief'] ? $sdf['brief'] : '',
			'marketable'=>($sdf['approve_status'] == 'onsale') ? 'true' : 'false',
			'thumbnail_pic'=>'',
			'description'=>$sdf['description'] ? $sdf['description'] : '',
		);
		if ($sdf['goods_id'])
			$sdf_goods['goods_id'] = $sdf['goods_id'];
		
		if ($goods_store)
			$sdf_goods['store'] = $goods_store;
			
		/** 货品信息 **/
		if ($sdf['sku_bns'])
		{
			$arr_sku_bns = json_decode($sdf['sku_bns']);
			$arr_sku_prices = json_decode($sdf['sku_prices']);
			$arr_suk_costs = json_decode($sdf['sku_costs']);
			$arr_sku_mktprices = json_decode($sdf['sku_mktprice']);
			foreach ($sdf['sku_bns'] as $key=>$bns)
			{
				$sdf_goods['product'][$key] = array(
					'price'=>array(
						'price'=>array(
							'price'=>$arr_sku_prices[$key] ? $arr_sku_prices[$key] : '',
						),
						'cost'=>array(
							'price'=>$arr_suk_costs[$key] ? $arr_suk_costs[$key] : '',
						),
						'mktprice'=>array(
							'price'=>$arr_sku_mktprices[$key] ? $arr_sku_mktprices[$key] : '',
						),
					),
					'name' => $sdf['name'],
					'weight'=>$sdf['weight'] ? $sdf['weight'] : '',
					'unit' => $sdf['unit'] ? $sdf['unit'] : '',
					'store' => $arr_sku_store[$key],
					'freez' => '0',
					'goods_type' => 'normal',
					'uptime' => $time,
					'last_modify' => $time,
				);
				if ($sdf['goods_id'])
					$sdf_goods['product'][$key]['goods_id'] = $sdf['goods_id'];
			}
		}
		else
		{
			$sdf_goods['product'] = array(				
				array(
					'bn' => $sdf['bn'],
					'price'=>array(
							'price'=>array(
								'price'=>$sdf['price'] ? $sdf['price'] : '',
							),
							'cost'=>array(
								'price'=>$sdf['cost'] ? $sdf['cost'] : '',
							),
							'mktprice'=>array(
								'price'=>$sdf['mktprice'] ? $sdf['mktprice'] : '',
							),
						),
					'name' => $sdf['name'],
					'weight'=>$sdf['weight'] ? $sdf['weight'] : '',
					'unit' => $sdf['unit'] ? $sdf['unit'] : '',
					'freez' => '0',
					'goods_type' => 'normal',
					'uptime' => $time,
					'last_modify' => $time,
				),
			);
			if ($goods_store)
				$sdf_goods['product'][0]['store'] = $goods_store;
			if ($sdf['goods_id'])
				$sdf_goods['product'][0]['goods_id'] = $sdf['goods_id'];
		}
		/** end **/
		if ($sdf['spec_properties'])
		{
			$arr_spec_properties = json_decode($sdf['spec_properties'], 1);
			if ($arr_spec_properties)
			{
				$arr_spec_value = array();
				$arr_spec_value_id = array();
				$arr_private_value_id = array();
				$obj_spec_value = $this->app->model('spec_values');
				foreach ($arr_spec_properties as $key=>$spec_value)
				{
					$tmp = $obj_spec_value->getList('*', array('spec_value_id'=>$spec_value));
					if (!$tmp)
					{
						$msg = app::get('b2c')->_('需要添加的商品的规格不存在！');
						return false;
					}
					$arr_spec_value[$key] = $tmp[0]['spec_value'];
					$arr_spec_value_id[$key] = $spec_value;	
					$arr_private_value_id[$key] = utils::microtime();
				}
				
				$sdf_goods['spec_desc'] = array(
					'spec_value'=>$arr_spec_value,
					'spec_private_value_id'=>$arr_private_value_id,
					'spec_value_id'=>$arr_spec_value_id,
				);
			}
		}
		/** 图片处理 **/
		if ($sdf['image_default_id'])
		{
			$mdl_img = app::get('image')->model('image');            
			$image_name = substr($sdf['image_default_id'], strrpos($sdf['image_default_id'],'/')+1);
			$image_id = $mdl_img->store($sdf['image_default_id'],null,null,$image_name);        
			$sdf_goods['image_default_id'] = $image_id;
		}
		/** 商品属性列表 **/
		if ($sdf['props'])
		{
			$arr_props = json_decode($sdf['props'], 1);
			if ($arr_props)
			{
				foreach ($arr_props as $key=>$value)
				{
					$sdf_goods['props']['p_'.$key]['value'] = $value;
				}
			}
		}
		/** 用户自行输入的类目属性 **/
		if ($sdf['input_pids'] && $sdf['input_str'])
		{
			$arr_input_pids = explode(',',$sdf['input_pids']);
			$arr_input_strs = explode(';',$sdf['input_str']);
			if (count($arr_input_pids) == count($arr_input_strs) && count($arr_input_strs) > 0)
			{
				foreach ($arr_input_pids as $key=>$input_id)
				{
					$sdf_goods['props']['p_'.$input_id]['value'] = $arr_input_strs[$key];
				}
			}
		}
		
		$obj_goods = $this->app->model('goods');
		$goods_id = $obj_goods->save($sdf_goods);
		if (!$goods_id)
		{
			$msg = app::get('b2c')->_('商品添加失败！');
			return false;
		}
		if ($sdf['goods_id'])
			$goods_id = $sdf['goods_id'];
		
		$is_save = true;
		// 生成推荐商品
		if ($sdf['related_items'])
		{
			$arr_related_items = json_decode($sdf['related_items'], 1);
			if ($arr_related_items)
			{
				$obj_goods_rate = $this->app->model('goods_rate');
				foreach ($arr_related_items as $related_item)
				{
					$filter = array(
						'filter_sql'=>'(`goods_1`="'.$goods_id.'" AND `goods_2`="'.$related_item.'") OR (`goods_1`="'.$related_item.'" AND `goods_2`="'.$goods_id.'")',
					);
					$tmp = $obj_goods_rate->getList('*',$filter);
					if (count($tmp) >= 2)
					{
						// 有两个left关联.
					}
					elseif (count($tmp) >=1)
					{
						if ($tmp[0]['goods_1'] == $goods_id)
							continue;
						
						$filter = array(
							'goods_1'=>$tmp[0]['goods_1'],
							'goods_2'=>$tmp[0]['goods_2'],
						);
						$is_save = $obj_goods_rate->update(array('manual'=>'both'),$filter);
					}
					else
					{
						$data = array(
							'goods_1'=>$goods_id,
							'goods_2'=>$related_item,
						);
						$is_save = $obj_goods_rate->insert($data);
					}
				}
			}
		}
		
		/** 商品关键字 **/
		if ($sdf['keywords'])
		{
			$arr_keywords = json_decode($sdf['keywords'], 1);
			if ($arr_keywords)
			{
				$obj_goods_rate = $this->app->model('goods_keywords');
				foreach ($arr_keywords as $str_keywords)
				{
					$data = array(
						'goods_id'=>$goods_id,
						'keyword'=>$str_keywords,
						'res_type'=>'goods',
					);
					$is_save = $obj_goods_rate->insert($data);
				}
			}
		}
		
		if (!$is_save)
		{
			$msg = app::get('b2c')->_('商品相关信息添加失败！');
			return false;
		}
		
		return $goods_id;
	}
	
	/**
	 * 删除一个商品
	 * @param mixed sdf结构
	 * @param object handle object
	 * @return mixed 返回增加的结果
	 */
	public function delete(&$sdf, &$thisObj)
	{
		if (!$sdf['goods_id'])
		{
			$thisObj->send_user_error(app::get('b2c')->_('需要修改的商品信息不存在！'), array('iid'=>''));
		}
		
		$db = kernel::database();
		$transaction_status = $db->beginTransaction();
		/**
		 * 删除商品对应的货品
		 */
		$obj_products = $this->app->model('products');
		$is_delete = $obj_products->delete(array('goods_id'=>$sdf['goods_id']));
		
		if (!$is_delete)
		{
			$db->rollback();
			$thisObj->send_user_error(app::get('b2c')->_('删除商品的货品失败！'), array('iid'=>$sdf['goods_id'],'modified'=>time()));
		}
		
		$db->commit($transaction_status);
		return array('iid'=>$sdf['goods_id'], 'modified'=>time());
	}
	
	/**
	 * 获取商品列表
	 * @param mixed sdf结构
	 * @param object handle object
	 * @return mixed 返回增加的结果
	 */
	public function get_all_list(&$sdf, &$thisObj)
	{
		if (!$sdf['page_no'] || !$sdf['page_size'])
		{
			$thisObj->send_user_error(app::get('b2c')->_('页面和分页数不存在！'), array('items'=>'','totalResults'=>0));
		}
		
		/** 生成过滤条件 **/
		$db = kernel::database();
		$condition = "";
		if ($sdf['cat_id'])
			$condition .= " AND cat_id=".intval($sdf['cat_id']);
		if ($sdf['cid'])
			$condition .= " AND type_id=".intval($sdf['cid']);
		if ($sdf['brand_id'])
			$condition .= " AND brand_id=".$sdf['brand_id'];
			
		$page_no = intval($sdf['page_no']) - 1;
		$page_size = intval($sdf['page_size']);
		
		if (!$rows = $db->select("SELECT * FROM `sdb_b2c_goods` WHERE 1".$condition." LIMIT ".$page_no.",".$page_size))
		{
			return array('total_results'=>0, 'items'=>'');
		}
		
		/**
		 * 得到返回的商品信息
		 */
		$sdf_goods = array();
		$obj_ctl_goods = kernel::single('b2c_ctl_site_product');
		foreach($rows as $arr_row)
		{
			$sdf_goods['item'][] = $this->get_item_detail($arr_row, $obj_ctl_goods);
		}
		
		return array('total_results'=>count($rows), 'items'=>json_encode($sdf_goods));
	}
	
	/**
	 * 获取商品信息
	 * @param mixed sdf结构
	 * @param object handle object
	 * @return mixed 返回增加的结果
	 */
	public function get_item(&$sdf, &$thisObj)
	{
		if (!$sdf['goods_id'])
		{
			$thisObj->send_user_error(app::get('b2c')->_('必填参数不存在！'), array('item'=>''));
		}
		
		$db = kernel::database();
		if (!$rows = $db->select("SELECT * FROM `sdb_b2c_goods` WHERE `goods_id`=".$sdf['iid']))
		{
			return array('item'=>'');
		}
		/**
		 * 得到返回的商品信息
		 */
		$sdf_goods = array();
		$obj_ctl_goods = kernel::single('b2c_ctl_site_product');
		$sdf_goods['item'] = json_encode($this->get_item_detail($rows[0], $obj_ctl_goods));
		
		return array('item'=>$sdf_goods['item']);
	}
	
	/**
	 * 生成商品明细的数组
	 * @param mixed 每行商品的数组-数据
	 * @param object goods controller
	 * @return mixed 
	 */
	private function get_item_detail($arr_row, $obj_clt_goods)
	{
		if (!$arr_row || !$obj_clt_goods)
			return array();
			
		$cnt_props = 20;
		$cn_input_props = 50;
		
		$detal_url = $obj_ctl_goods->gen_url(array('app'=>'b2c','ctl'=>'site_product','act'=>'index','arg0'=>$arr_row['goods_id']));
		/** props 目前是1-20 **/
		$props = "";
		for ($i=1;$i<=$cnt_props;$i++)
		{
			if ($arr_row['p_'.$i])
				$props .= $i.":".$arr_row['p_'.$i].";";
		}
		if ($props)
			$props = substr($props, 0, strlen($props)-1);
		/** end **/
		
		/** input props 21-50 **/
		$input_pids = "";
		$input_str = "";
		for ($i=$cnt_props+1;$i<=$cn_input_props;$i++)
		{
			if ($arr_row['p_'.$i])
			{
				$input_pids .= $i.",";
				$input_str .= $arr_row['p_'.$i].";";
			}
		}
		if ($input_pids)
			$input_pids = substr($input_pids, 0, strlen($input_pids)-1);
		if ($input_str)
			$input_str = substr($input_str, 0, strlen($input_str)-1);
		/** end **/
		$db = kernel::database();
		$arr_skus = $db->select("SELECT * FROM `sdb_b2c_products` WHERE `goods_id`=".$arr_row['goods_id']);
		$arr_sdf_skus = array();
		$str_skus = "";
		if ($arr_skus)
		{
			foreach ($arr_skus as $arr)
			{
				$str_skus_properties = '';
				$arr_spec_desc = unserialize($arr['spec_desc']);
				if($arr_spec_desc['spec_value_id'])
				{
					foreach ($arr_spec_desc['spec_value_id'] as $arr_value)
						$str_skus_properties .= $arr_value . ':' . $arr_spec_desc['spec_value'] . ";";
				}
				if ($str_skus_properties)
					$str_skus_properties = substr($str_skus_properties, 0, strlen($str_skus_properties)-1);
				
				$arr_sdf_skus[] = array(
					'sku_id'=>$arr['product_id'],
					'iid'=>$arr['goods_id'],
					'bn'=>$arr['bn'],
					'properties'=>$str_skus_properties,
					'quantity'=>$arr['store'],
					'weight'=>$arr['weight'],
					'price'=>$arr['price'],
					'market_price'=>$arr['mktprice'],
					'modified'=>$arr['last_modify'],
					'cost'=>$arr['cost'],
				);
			}
			$str_skus = json_encode($arr_sdf_skus);
		}
		$default_img_url = kernel::single('base_storager')->image_path($arr_row['image_default_id']);
		
		return array(
			'iid'=>$arr_row['goods_id'],
			'title'=>$arr_row['name'],
			'bn'=>$arr_row['bn'],
			'shop_cids'=>$arr_row['cat_id'],
			'cid'=>$arr_row['type_id'],
			'brand_id'=>$arr_row['brand_id'],
			'props'=>$props,
			'input_pids'=>$input_pids,
			'input_str'=>$input_str,
			'description'=>$arr_row['intro'],
			'default_img_url'=>$default_img_url,
			'num'=>$arr_row['store'],
			'weight'=>$arr_row['weight'] ? $arr_row['weight'] : '',
			'score'=>$arr_row['score'] ? $arr_row['score'] : '',
			'price'=>$arr_row['price'],
			'market_price'=>$arr_row['mktprice'],
			'unit'=>$arr_row['unit'],
			'cost_price'=>$arr_row['cost'],
			'modified'=>date('Y-m-d H:i:s',$arr_row['last_modify']),
			'list_time'=>date('Y-m-d H:i:s',$arr_row['uptime']),
			'delist_time'=>date('Y-m-d H:i:s',$arr_row['downtime']),
			'created'=>date('Y-m-d H:i:s',$arr_row['last_modify']),
			'skus'=>$str_skus,
		);
	}
	
	/**
	 * 编辑货品
	 * @param mixed sdf结构
	 * @param object handle object
	 * @return mixed 返回增加的结果
	 */
	public function update_sku(&$sdf, &$thisObj)
	{
		if (!$sdf['goods_id'] || !$sdf['bn'])
		{
			$thisObj->send_user_error(app::get('b2c')->_('商品ID或货品bn为空！'), array('item'=>''));
		}
		
		$obj_products = $this->app->model('products');
		$sdf_products = array();
		$time = array();
		if ($sdf['store'])
			$sdf_products['store'] = $sdf['store'];
		if ($sdf['weight'])
			$sdf_products['weight'] = $sdf['weight'];
		if ($sdf['mktprice'])
			$sdf_products['mktprice'] = $sdf['mktprice'];
		if ($sdf['price'])
			$sdf_products['price'] = $sdf['price'];
		if ($sdf['cost'])
			$sdf_products['cost'] = $sdf['cost'];
		
		if ($sdf_products)
		{
			if (!$obj_products->update($sdf_products, array('goods_id'=>$sdf['goods_id'],'bn'=>$sdf['bn'])))
			{
				$thisObj->send_user_error(app::get('b2c')->_('货品信息更新失败！'), array('item'=>''));
			}
		}
		
		$tmp = $obj_products->getList('product_id', array('goods_id'=>$sdf['goods_id'],'bn'=>$sdf['bn']));			
		return array('iid'=>$sdf['goods_id'],'sku_id'=>$tmp[0]['product_id'],'sku_bn'=>$sdf['bn'],'modified'=>$time);
	}
	
	/**
	 * 获取货品信息
	 * @param mixed sdf结构
	 * @param object handle object
	 * @return mixed 返回增加的结果
	 */
	public function get_sku(&$sdf, &$thisObj)
	{
		if (!$sdf['goods_id'] || !$sdf['bn'])
		{
			$thisObj->send_user_error(app::get('b2c')->_('商品ID或货品bn为空！'), array('item'=>''));
		}
		

		$obj_product = $this->app->model('product');
		$arr_product = $obj_product->getList('*', array('goods_id'=>$sdf['goods_id'],'bn'=>$sdf['bn']));
		if (!$arr_product)
			return array();			
		$arr = $arr_product[0];
		
		/** 组成返回数组 **/
		$str_skus_properties = '';
		$arr_spec_desc = unserialize($arr['spec_desc']);
		if($arr_spec_desc['spec_value_id'])
		{
			foreach ($arr_spec_desc['spec_value_id'] as $arr_value)
				$str_skus_properties .= $arr_value . ':' . $arr_spec_desc['spec_value'] . ";";
		}
		if ($str_skus_properties)
			$str_skus_properties = substr($str_skus_properties, 0, strlen($str_skus_properties)-1);
		
		return array(
			'sku_id'=>$arr['product_id'],
			'iid'=>$arr['goods_id'],
			'bn'=>$arr['bn'],
			'properties'=>$str_skus_properties,
			'quantity'=>$arr['store'],
			'weight'=>$arr['weight'],
			'price'=>$arr['price'],
			'market_price'=>$arr['mktprice'],
			'modified'=>$arr['last_modify'],
			'cost'=>$arr['cost'],
		);
	}
	
	/**
	 * 获取商品类型
	 * @param mixed sdf结构
	 * @param object handle object
	 * @return mixed 返回增加的结果
	 */
	public function get_types(&$sdf, &$thisObj)
	{
		if (!$sdf['page_no'] || !$sdf['page_size'])
		{
			$page_no = 1;
			$page_size = 40;
		}
		
		$page_no = intval($sdf['page_no']) - 1;
		$page_size = intval($sdf['page_size']);
		
		$db = kernel::database();
		$arr_goods_types = $db->select("SELECT * FROM `sdb_b2c_goods_type` LIMIT ".$page_no.",".$page_size."");
		if (!$arr_goods_types)
			return array();
		
		$arr_sdf_goods_types = array();
		foreach ($arr_goods_types as $index=>$arr)
		{
			$arr_sdf_goods_types[$index] = array(
				'type_id'=>$arr['type_id'],
				'name'=>$arr['name'],
				'brands'=>$arr_brand_ids,
				'setting'=>unserialize($arr['setting']),
			);
			
			/** brand id array **/
			$arr_brand_ids = $db->select("SELECT `brand_id` FROM `sdb_b2c_type_brand` WHERE `type_id`=".$arr['type_id']);
			$arr_sdf_goods_types[$index]['brands'] = $arr_brand_ids;
			/** properties array **/
			$arr_props = $db->select("SELECT `props_id`,`type`,`name` FROM `sdb_b2c_goods_type_props` WHERE `type_id`=".$arr['type_id']);
			$arr_prop_names = array();
			$arr_prop_types = array();
			$arr_prop_options = array();
			foreach ($arr_props as $arr_prop)
			{
				$arr_prop_names[] = $arr_prop['name'];
				$arr_prop_types[] = $arr_prop['type'];
				$arr_props = $db->select("SELECT `name` FROM `sdb_b2c_goods_type_props_value` WHERE `props_id`=".$arr_prop['props_id']);
				$arr_prop_options = $arr_props;
			}
			$arr_sdf_goods_types[$index]['props'] = array(
				'name'=>$arr_prop_names,
				'type'=>$arr_prop_types,
				'options'=>$arr_prop_options,
			);
			/** specialist array **/
			$arr_specifications = $db->select("SELECT `spec_id`,`spec_type` FROM `sdb_b2c_specification` WHERE `type_id` IN (SELECT `spec_id` FROM `sdb_b2c_goods_type_spec` WHERE `type_id`=".$arr['type_id'].")");
			$arr_spec_ids = array();
			$arr_spec_types = array();
			foreach ((array)$arr_specifications as $arr_specs)
			{
				$arr_spec_ids[] = $arr_specs['spec_id'];
				$arr_spec_types[] = $arr_specs['spec_type'];
			}
			$arr_sdf_goods_types[$index]['spec'] = array(
				'id'=>$arr_spec_ids,
				'type'=>$arr_spec_types,
			);
			/** params array **/
			$arr_goods_type_params = unserialize($arr['params']);
			$arr_goods_type_param_groups = array();
			$arr_goods_type_param_names = array();
			foreach ((array)$arr_goods_type_params as $key=>$arr_goods_type_param)
			{
				$arr_goods_type_param_groups[] = $key;
				$arr_keys = array_keys($arr_goods_type_param);
				$arr_goods_type_param_names[] = $arr_keys[0];
			}
			$arr_sdf_goods_types[$index]['params'] = array(
				'group'=>$arr_goods_type_param_groups,
				'name'=>$arr_goods_type_param_names,
			);
			/** minfo 商品必填项 **/
			$arr_goods_type_minfos = unserialize($arr['minfo']);
			foreach ((array)$arr_goods_type_minfos as $key=>$arr_goods_type_minfo)
			{
				$arr_sdf_goods_types[$index]['minfo'] = array(
					'label'=>$arr_goods_type_minfo['label'],
					'type'=>$arr_goods_type_minfo['type'],
					'options'=>array(),
				);
				if ($arr_goods_type_minfo['type'] == 'select')
				{
					$arr_sdf_goods_types[$index]['minfo']['options'] = $arr_goods_type_minfo['options'];
				}
			}
		}
		
		return array('datas'=>$arr_sdf_goods_types,'totalResults'=>count($arr_sdf_goods_types));
	}
	
	/**
	 * 获取商品分类 - 直连方法
	 * @param mixed sdf结构
	 * @param object handle object
	 * @return mixed 返回增加的结果
	 */
	public function get_cat(&$sdf, &$thisObj)
	{
		if (!$sdf)
			return array();
		
		if (!$sdf['page_no'] || !$sdf['page_size'])
		{
			$page_no = 1;
			$page_size = 40;
		}
		
		$page_no = intval($sdf['page_no']) - 1;
		$page_size = intval($sdf['page_size']);
		
		$db = kernel::database();
		$arr_goods_cats = $db->select("SELECT * FROM `sdb_b2c_goods_cat` LIMIT ".$page_no.",".$page_size."");
		if (!$arr_goods_cats)
			return array();
		
		$sdf_arr_goods_cats = array();
		foreach ($arr_goods_cats as $index=>$arr_cat)
		{
			$arr_goods_type =  $db->selectrow("SELECT `name` FROM `sdb_b2c_goods_type` WHERE `type_id`=".$arr_cat['type_id']);
			$sdf_arr_goods_cats[$index] = array(
				'cat_name'=>$arr_cat['cat_name'],
				'cat_id'=>$arr_cat['cat_id'],
				'pid'=>$arr_cat['parent_id'],
				'type'=>$arr_cat['type_id'],
				'type_name'=>$arr_goods_type['name'],
				'p_order'=>$arr_cat['p_order'],
				'cat_path'=>$arr_cat['cat_path'],
				'is_leaf'=>$arr_cat['is_leaf'],
			);
		}
		
		return array('datas'=>$arr_sdf_goods_types,'totalResults'=>count($sdf_arr_goods_cats));
	}
	
	/**
	 * 获取品牌 - 直连方法
	 * @param mixed sdf结构
	 * @param object handle object
	 * @return mixed 返回增加的结果
	 */
	public function get_brands(&$sdf, &$thisObj)
	{
		if (!$sdf)
			return array();
		
		if (!$sdf['page_no'] || !$sdf['page_size'])
		{
			$page_no = 1;
			$page_size = 40;
		}
		
		$page_no = intval($sdf['page_no']) - 1;
		$page_size = intval($sdf['page_size']);
		
		$db = kernel::database();
		$arr_goods_brands = $db->select("SELECT * FROM `sdb_b2c_brand` LIMIT ".$page_no.",".$page_size."");
		if (!$arr_goods_brands)
			return array();
		
		$sdf_arr_goods_brands = array();
		foreach ($sdf_arr_goods_brands as $index=>$arr_brand)
		{
			$sdf_arr_goods_brands[$index] = array(
				'brand_id'=>$arr_brand['brand_id'],
				'brand_name'=>$arr_brand['brand_name'],
				'brand_logo'=>$arr_brand['brand_logo'],
				'brand_url'=>$arr_brand['brand_url'],
			);
		}
		
		return array('datas'=>$sdf_arr_goods_brands,'totalResults'=>count($sdf_arr_goods_brands));
	}
	
	/**
	 * 获取规格 - 直连方法
	 * @param mixed sdf结构
	 * @param object handle object
	 * @return mixed 返回增加的结果
	 */
	public function get_specs(&$sdf, &$thisObj)
	{
		if (!$sdf)
			return array();
		
		if (!$sdf['page_no'] || !$sdf['page_size'])
		{
			$page_no = 1;
			$page_size = 40;
		}
		
		$page_no = intval($sdf['page_no']) - 1;
		$page_size = intval($sdf['page_size']);
		
		$db = kernel::database();
		$arr_goods_specifications = $db->select("SELECT * FROM `sdb_b2c_specification` LIMIT ".$page_no.",".$page_size."");
		if (!$arr_goods_specifications)
			return array();
		
		$sdf_arr_goods_specifications = array();
		foreach ($arr_goods_specifications as $index=>$arr_spec)
		{
			$arr_goods_spec_values = $db->select("SELECT * FROM `sdb_b2c_spec_values` WHERE `spec_id`=".$arr_spec['spec_id']);
			$arr_value_ids = array();
			$arr_values = array();
			$arr_value_imgs = array();
			foreach ($arr_goods_spec_values as $arr)
			{
				$arr_value_ids[] = $arr['spec_value_id'];
				$arr_values[] = $arr['spec_value'];
				$arr_value_imgs[] = $arr['spec_image'];
			}
			$sdf_arr_goods_specifications[$index] = array(
				'spec_id'=>$arr_spec['spec_id'],
				'spec_name'=>$arr_spec['spec_name'],
				'spec_show_typ'=>$arr_spec['spec_show_typ'],
				'spec_type'=>$arr_spec['spec_type'],
				'val'=>$arr_value_ids,				
				'spec_value'=>$arr_values,
				'spec_image'=>$arr_value_imgs,
			);
		}
		
		return array('datas'=>$sdf_arr_goods_specifications,'totalResults'=>count($sdf_arr_goods_specifications));
	}
}